﻿package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.Cargo;
import com.edward.model.DT_Cargo;
import com.edward.model.TransferList;
import com.edward.service.CargoManager;
import com.edward.service.DT_CargoManager;
import com.edward.service.TransferListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TransferListAction extends ActionSupport {


	private static final long serialVersionUID = -6667921891650652782L;

	private TransferListManager transferListManager;
	
	private DT_CargoManager dt_CargoManager;

	private CargoManager cargoManager;
	
	private TransferList transferList;
	
	private DT_Cargo dt_Cargo;
	
	private Cargo cargo;
	
	private int id;
	
	private int limit;
	
	private int start;
	
	private String query;
	
	private boolean oversea = false;


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}

	
	public boolean isOversea() {
		return oversea;
	}


	public void setOversea(boolean oversea) {
		this.oversea = oversea;
	}


	public TransferList getTransferList() {
		return transferList;
	}


	public void setTransferList(TransferList transferList) {
		this.transferList = transferList;
	}


	public DT_Cargo getDt_Cargo() {
		return dt_Cargo;
	}


	public void setDt_Cargo(DT_Cargo dtCargo) {
		dt_Cargo = dtCargo;
	}


	public void setTransferListManager(TransferListManager transferListManager) {
		this.transferListManager = transferListManager;
	}


	public void setDt_CargoManager(DT_CargoManager dtCargoManager) {
		dt_CargoManager = dtCargoManager;
	}
	
	public Cargo getCargo() {
		return cargo;
	}


	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}


	public void setCargoManager(CargoManager cargoManager) {
		this.cargoManager = cargoManager;
	}
	
	public int getLimit() {
		return limit;
	}


	public void setLimit(int limit) {
		this.limit = limit;
	}


	public int getStart() {
		return start;
	}


	public void setStart(int start) {
		this.start = start;
	}
	
	public String getQuery() {
		return query;
	}


	public void setQuery(String query) {
		this.query = query;
	}

	//添加saleList 的表头
	@Secured("ROLE_DEPOT_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		transferList.setListNum(transferListManager.listNum());
		UserDetails userDetails = (UserDetails) SecurityContextHolder
		.getContext().getAuthentication().getPrincipal();
		transferList.setAuditor(transferListManager.getUserName(userDetails.getUsername()));
		transferListManager.addTransferList(transferList);
		int total = transferListManager.listTransferList(oversea).size();
		response.getWriter().print("{success : true , msg : '添加成功!',transferList_id:"+ transferList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		TransferList t = transferListManager.getTransferList(transferList.getId());
		transferList.setGoods(t.getGoods());
		transferList.setAuditor(t.getAuditor());
		transferListManager.updateTransferList(transferList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		TransferList t = transferListManager.getTransferList(transferList.getId());
		if(t.getGoods().size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在销售货物列表，请先删除销售货物!'}");
		}else{
			transferListManager.deleteTransferList(t.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}

	@Secured("ROLE_DEPOT")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		pager = transferListManager.pagerList(start, limit,oversea);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	
	
	//添加transferList 的 货物明细
	@Secured("ROLE_DEPOT_ADD")
	public String addDTCargo() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		TransferList t = transferListManager.getTransferList(transferList.getId());
		cargo.setCount(dt_Cargo.getAmount());
		Cargo c = cargoManager.checkCargo(t.getWarehouse(), cargo.getCode());
		if(c.getCount()-cargo.getCount()<0){
			response.getWriter().print("{success:false, msg : '添加失败，此货物在该仓库中库存不足!'}");
			return null;
		}
		c.setCount(c.getCount()-cargo.getCount());
		cargoManager.updateCargo(c);
		dt_Cargo.setCargo(c);
		dt_Cargo.setSum(dt_Cargo.getPrice() * dt_Cargo.getAmount());
		dt_CargoManager.addDT_Cargo(dt_Cargo);
		t.getGoods().add(dt_Cargo);
		transferListManager.updateTransferList(t);
		
		//在转入的仓库添加该货物
		Cargo inCargo = cargoManager.checkCargo(t.getInWarehouse(), cargo.getCode());
		if(inCargo == null){
			c.setId(0);
			c.setCount(dt_Cargo.getAmount());
			c.setLocation(t.getInWarehouse());
			cargoManager.addCargo(c);
		}else{
			inCargo.setCount(inCargo.getCount() + dt_Cargo.getAmount());
			cargoManager.updateCargo(inCargo);
		}
		
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String updateDTCargo() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DT_Cargo dt = dt_CargoManager.getDT_Cargo(dt_Cargo.getId());
	
		
		
		//由于变更的cargo没有Id ,先从数据库中获取其ID 再一同更新
		Cargo c =dt.getCargo();
		int i = c.getCount() - dt_Cargo.getAmount() + dt.getAmount();
		if(i<0){
			response.getWriter().print("{success:false, msg : '添加失败，此货物在该仓库中库存不足!'}");
			return null;
		}
		
		//更新转入该仓库的货物数量
		TransferList t = transferListManager.getTransferList(transferList.getId());
		Cargo inCargo = cargoManager.checkCargo(t.getInWarehouse(), cargo.getCode());
		inCargo.setCount(inCargo.getCount() + dt_Cargo.getAmount() - dt.getAmount());
		cargoManager.updateCargo(inCargo);
		
		//更新原有仓库的货物信息
		c.setCount(i);
		dt_Cargo.setCargo(c);
		dt_Cargo.setSum(dt_Cargo.getPrice() * dt_Cargo.getAmount());
		cargoManager.updateCargo(c);
		dt_CargoManager.updateDT_Cargo(dt_Cargo);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String deleteDTCargo() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DT_Cargo dt = dt_CargoManager.getDT_Cargo(dt_Cargo.getId());
		Cargo c =dt.getCargo();
		c.setCount(c.getCount() + dt.getAmount() );
		cargoManager.updateCargo(c);
		//更新转入该仓库的货物数量
		TransferList t = transferListManager.getTransferList(transferList.getId());
		Cargo inCargo = cargoManager.checkCargo(t.getInWarehouse(), c.getCode());
		inCargo.setCount(inCargo.getCount()- dt.getAmount());
		cargoManager.updateCargo(inCargo);
		dt_CargoManager.deleteDT_Cargo(dt_Cargo.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	//获取grid 显示 的 分页信息
	@Secured("ROLE_DEPOT")
	public String getDTCargo() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = dt_CargoManager.pagerDTCargo(start, limit,transferList.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_DEPOT")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
}
